﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web;

namespace JieBooLaw.Common.Web.Util
{
    public class ScriptUtil
    {
        public static void AddScriptFile(Page page, string scriptFilePath, string id)
        {
            bool hasStype = false;
            for (int i = 0; i < page.Header.Controls.Count; i++)
            {
                if (page.Header.Controls[i].GetType() == typeof(HtmlGenericControl))
                {
                    if (((HtmlGenericControl)page.Header.Controls[i]).Attributes["src"].IndexOf(scriptFilePath, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        hasStype = true;
                    }
                }
            }
            if (!hasStype)
            {
                System.Web.UI.HtmlControls.HtmlGenericControl js = new System.Web.UI.HtmlControls.HtmlGenericControl();
                js.ID = id;
                js.TagName = "script";
                js.Attributes.Add("src", scriptFilePath);
                js.Attributes.Add("type", "text/javascript");
                js.Attributes.Add("language", "javascript");
                page.Header.Controls.AddAt(0, js);
            }
        }

        public static void AddStyleFile(Page page, string styleFile, string id)
        {
            bool hasStype = false;
            for (int i = 0; i < page.Header.Controls.Count; i++)
            {
                if (page.Header.Controls[i].GetType() == typeof(HtmlLink))
                {
                    if (((HtmlLink)page.Header.Controls[i]).Href.IndexOf(styleFile, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        hasStype = true;
                    }
                }
            }
            if (!hasStype)
            {
                HtmlLink link = new HtmlLink();
                link.ID = id;
                link.Attributes.Add("href", styleFile);
                link.Attributes.Add("type", "text/css");
                link.Attributes.Add("rel", "stylesheet");
                page.Header.Controls.Add(link);
            }
        }

        public static void RegisterStartupScript(string strScript)
        {
            Page p = HttpContext.Current.Handler as Page;
            System.Web.UI.ScriptManager sm = System.Web.UI.ScriptManager.GetCurrent(p);
            if (sm != null)
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(p, p.GetType(), DateTime.Now.Ticks.ToString(), strScript, true);
            }
            else
            {
                p.ClientScript.RegisterStartupScript(p.GetType(), DateTime.Now.Ticks.ToString(), strScript, true);
            }
        }

        public static void RegisterScriptBlock(string strScript)
        {
            Page p = HttpContext.Current.Handler as Page;
            System.Web.UI.ScriptManager sm = System.Web.UI.ScriptManager.GetCurrent(p);
            if (sm != null)
            {
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(p, p.GetType(), DateTime.Now.Ticks.ToString(), strScript, true);
            }
            else
            {
                p.ClientScript.RegisterClientScriptBlock(p.GetType(), DateTime.Now.Ticks.ToString(), strScript, true);
            }
        }
    }
}
